/**
 * 请求拦截
 * @param {Object} http
 */
import {
    useToken
} from "@/stores/userStores";
// import whtMd5 from '@/uni_modules/wht-md5/js_sdk/wht-md5.js'
import random from '../random.js';

export const requestInterceptors = (vm) => {
    uni.$uv.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
        config.data = config.data || {}
        const userToken = useToken();
        if (userToken.token) {
            config.header = {
                Authorization: 'Bearer ' + userToken.token
            }
        }
        //sign鉴权
        // let authToken = 'gX2gEoIJmx4GejK8NL85vjF70VmK3apb',
        //     timestamp = new Date().getTime(),
        //     nonce = random.random_char(32),
        //     data_for_sign = 'timestamp=' + timestamp + '&nonce=' + nonce;
        // const secreted = whtMd5(data_for_sign + authToken);
        config.url = config.url
        // #ifdef APP
        let push_cid = uni.getStorageSync('push_cid') || ''
        if (push_cid) {
            config.data = {
                push_cid: push_cid,
                ...config.data
            }
        }
        // #endif
        return config
    }, (config) => // 可使用async await 做异步操作
        Promise.reject(config))
}
